package cn.gitbook.securityshare.common;

import lombok.Data;

import java.io.Serializable;

import static cn.gitbook.securityshare.common.CodeMsg.SUCCESS;

@Data
public class JsonObject<T> implements Serializable {
    private int code;

    private T data;

    private String message;

    public JsonObject() {
    }

    public JsonObject(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public JsonObject(int code, T data) {
        this(code, data, "");
    }

    public JsonObject(CodeMsg codeMsg) {
        this(codeMsg.getCode(), null, codeMsg.getMessage());
    }

    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> JsonObject<T> success(T data) {
        return new JsonObject<>(SUCCESS.getCode(), data,SUCCESS.getMessage() );
    }

    /**
     * 失败
     *
     * @param errorCode
     * @return
     */
    public static JsonObject error(CodeMsg errorCode) {
        return new JsonObject<>(errorCode);
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @return
     */
    public static JsonObject error(int code, String message) {
        return new JsonObject(code, null, message);
    }

    /**
     * 失败
     *
     * @param errorCode
     * @return
     */
    public static JsonObject error(CodeMsg errorCode, String message) {
        return new JsonObject(errorCode.getCode(), null, message);
    }
}
